package com.cuitrip.business.tripservice;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.cuitrip.apiservice.OrderBusiness;
import com.cuitrip.apiservice.ServiceBusiness;
import com.cuitrip.apiservice.model.ApiManager;
import com.cuitrip.business.home.trip.TripServiceListFragment;
import com.cuitrip.business.login.LoginInstance;
import com.cuitrip.business.setting.SubmitSuccessActivity;
import com.cuitrip.business.setting.model.SubmitSuccessModel;
import com.cuitrip.business.tripservice.event.ServiceChangeEvent;
import com.cuitrip.business.tripservice.model.FormType;
import com.cuitrip.business.tripservice.model.ServiceInfo;
import com.cuitrip.business.tripservice.proxy.ServiceFormProxy;
import com.cuitrip.business.tripservice.ui.IconTextView;
import com.cuitrip.business.user.ChooseCityActivity;
import com.cuitrip.business.user.CompleteBasicUserInfoActivity;
import com.cuitrip.business.user.model.CtUserInfo;
import com.cuitrip.business.webview.H5UrlMaker;
import com.cuitrip.business.webview.proxy.WebViewProxy;
import com.cuitrip.component.choicedialog.ChoiceDialog;
import com.cuitrip.component.choicedialog.MultiItem;
import com.cuitrip.component.item.ItemLayout;
import com.cuitrip.service.R;
import com.cuitrip.util.JumpSuccessUtil;
import com.cuitrip.util.ResourceUtil;
import com.cuitrip.util.track.TrackUtil;
import com.lab.app.BaseActivity;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragment;
import com.lab.jumper.FragmentJumper;
import com.lab.network.model.CtApiResponse;
import com.lab.network.model.IProxyCallback;
import com.lab.network.model.ProxyResult;
import com.lab.network.proxy.ApiProxy;
import com.lab.rtenvironment.AppCoreRuntime;
import com.lab.rx.bus.RxBus;
import com.lab.utils.ArrayUtil;
import com.lab.utils.ImageHelper;
import com.lab.utils.MessageUtils;
import com.lab.utils.ToastUtil;
import com.lab.utils.Utils;
import com.lab.utils.imagechooser.EasyImageChooser;
import com.lab.widget.imageselector.MultiImageSelectorActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by sheena on 16/4/26.
 */
public class CreateTripEditMainFragment extends CustomUiFragment implements IProxyCallback {

    public static final int REQUEST_IMAGE_CODE = 0x01;


    public static final int PREVIEW_ACTION = 0x01;
    public static final int DRAFT_ACTION = 0x02;
    public static final int COMMIT_ACTION = 0x03;

    public static final int TYPE_VALIDATE_ALL=3;
    public static final int TYPE_VALIDATE_BASIC_INFO=0;
    public static final int TYPE_VALIDATE_MOBILE=1;
    public static final int TYPE_VALIDATE_ID=2;


    public static final String EDIT_KEY = "edit";


    @Bind(R.id.il_trip_title)
    ItemLayout tripTitleLayout;

    @Bind(R.id.il_trip_location)
    ItemLayout tripLocationLayout;

    @Bind(R.id.il_trip_cates)
    ItemLayout tripCatesLayout;

    @Bind(R.id.il_trip_desc)
    ItemLayout tripDescLayout;

    @Bind(R.id.il_trip_price)
    ItemLayout tripPriceLayout;

    @Bind(R.id.il_trip_other)
    ItemLayout tripOtherLayout;

    @Bind(R.id.tv_agree_term)
    TextView tvAgreeTerm;

    @Bind(R.id.commit)
    TextView tvCommit;

    @Bind(R.id.agree_icon)
    IconTextView agreeIcon;

    @Bind(R.id.main_pic)
    ImageView mMainPic;

    @Bind(R.id.main_pic_icon)
    IconTextView mMainPicIcon;

    @Bind(R.id.main_pic_layout)
    FrameLayout mMainPicLayout;

    @Bind(R.id.full_content)
    LinearLayout mFullContent;

    @Bind(R.id.scroll_view)
    ScrollView mScrollView;

    private boolean isAgree = true;
    private ServiceInfo mServiceInfo;
    private TripEditActivity.ContentMode contentMode = TripEditActivity.ContentMode.NEW_MODE;
    private int mSaveAction;
    private ApiProxy mApiProxy = new ApiProxy(this);
    private CtUserInfo userInfo;

    public static final int REQUEST_CODE_COMPLETE_POP=0x01;
    private Subscription subscription;

    public static CreateTripEditMainFragment newInstance(ServiceInfo serviceInfo) {
        CreateTripEditMainFragment tripEditMainFragment = new CreateTripEditMainFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(ServiceInfo.KEY, serviceInfo);
        tripEditMainFragment.setArguments(bundle);
        return tripEditMainFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle data = getArguments();
        if (data != null) {
            if (data.getBoolean(TripEditActivity.SERVICE_EDIT_MODE_KEY, false)) {
                contentMode = TripEditActivity.ContentMode.EDIT_MODE;
            }
            if (data.containsKey(ServiceInfo.KEY)) {
                mServiceInfo = (ServiceInfo) data.getSerializable(ServiceInfo.KEY);
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState, R.layout.page_create_trip);
        subscription = RxBus.getDefault().toObservable().subscribe(new Action1<Object>() {
            @Override
            public void call(Object o) {
                if (o instanceof ServiceChangeEvent) {
                    ServiceChangeEvent serviceChangeEvent = (ServiceChangeEvent) o;
                    handleServiceChange(serviceChangeEvent.type);
                }

            }
        });
        return view;
    }

    private void handleServiceChange(FormType type) {
        if (!isAdded()) {
            return;
        }
        switch (type) {
            case MAIN_PIC:
                renderMainPic();
                break;
            case ADDRESS:
                renderAddress();
                break;
            case TITLE:
                renderTitle();
                break;
            case CATEGORY:
                renderCategory();
                break;
            case PAY:
                renderPay();
                break;
            case DESCRIPTION:
                renderDescription();
                break;
            case OTHERS:
                renderOtherInstruction();
                break;

        }

    }


    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config = new CustomUiConfig();
        if (contentMode.equals(TripEditActivity.ContentMode.EDIT_MODE)) {
            config.mTopbarMiddleAreaName = getResources().getString(R.string.operation_edit);
        } else {
            config.mTopbarMiddleAreaName = getResources().getString(R.string.trip_operation_create);
        }
        config.mTopbarLeftAreaName = getString(R.string.back_icon);
        config.mTopbarRightAreaName = getString(R.string.operation_preview);
        config.mRightStyle = CustomUiConfig.AreaStyle.TEXT_STYLE;
        return config;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this, getView());
    }

    @Override
    public void initData() {
        tvAgreeTerm.setText(getString(R.string.term_operation_agree, getString(R.string.term_service_en_fi)));
        renderTitle();
        renderAddress();
        renderPay();
        renderDescription();
        renderOtherInstruction();
        renderCategory();
        renderMainPic();
        judgeBasicUserInfo();
    }

    public void judgeBasicUserInfo() {
        showLoading();
        OrderBusiness.checkBasicUserInfo(mApiProxy,"1");
    }

    private void renderAddress() {
        if (ServiceFormProxy.getInstance().isFormComplete(mServiceInfo, FormType.ADDRESS)) {
            tripLocationLayout.setLeftIconColor(ResourceUtil.getColor(R.color.apple_7ecc1e));
        } else {
            tripLocationLayout.setLeftIconColor(ResourceUtil.getColor(R.color.ganshi_ded8d7));
        }
    }

    public void renderMainPic() {
        if (ServiceFormProxy.getInstance().isFormComplete(mServiceInfo, FormType.MAIN_PIC) && !ArrayUtil.isNullOrEmpty(mServiceInfo.getPictureDescriptionList())) {
            mMainPic.setVisibility(View.VISIBLE);
            ImageHelper.displayCtImage(mServiceInfo.getPictureDescriptionList().get(0).getUrl(), mMainPic, null);
        } else {
            mMainPic.setVisibility(View.GONE);
        }
    }

    private void renderTitle() {
        if (ServiceFormProxy.getInstance().isFormComplete(mServiceInfo, FormType.TITLE)) {
            tripTitleLayout.setLeftIconColor(ResourceUtil.getColor(R.color.apple_7ecc1e));
        } else {
            tripTitleLayout.setLeftIconColor(ResourceUtil.getColor(R.color.ganshi_ded8d7));
        }
    }

    private void renderPay() {
        if (ServiceFormProxy.getInstance().isFormComplete(mServiceInfo, FormType.PAY)) {
            tripPriceLayout.setLeftIconColor(ResourceUtil.getColor(R.color.apple_7ecc1e));
        } else {
            tripPriceLayout.setLeftIconColor(ResourceUtil.getColor(R.color.ganshi_ded8d7));
        }
    }

    private void renderDescription() {
        if (ServiceFormProxy.getInstance().isFormComplete(mServiceInfo, FormType.DESCRIPTION)) {
            tripDescLayout.setLeftIconColor(ResourceUtil.getColor(R.color.apple_7ecc1e));
        } else {
            tripDescLayout.setLeftIconColor(ResourceUtil.getColor(R.color.ganshi_ded8d7));
        }
    }

    private void renderCategory() {
        if (ServiceFormProxy.getInstance().isFormComplete(mServiceInfo, FormType.CATEGORY)) {
            tripCatesLayout.setLeftIconColor(ResourceUtil.getColor(R.color.apple_7ecc1e));
        } else {
            tripCatesLayout.setLeftIconColor(ResourceUtil.getColor(R.color.ganshi_ded8d7));
        }
    }

    private void renderOtherInstruction() {
        if (ServiceFormProxy.getInstance().isFormComplete(mServiceInfo, FormType.OTHERS)) {
            tripOtherLayout.setLeftIconColor(ResourceUtil.getColor(R.color.apple_7ecc1e));
        } else {
            tripOtherLayout.setLeftIconColor(ResourceUtil.getColor(R.color.ganshi_ded8d7));
        }
    }


    @OnClick(R.id.il_trip_title)
    public void modifyTitle() {
        CreateTripTitleFragment createTripTitleFragment = CreateTripTitleFragment.newInstance(mServiceInfo, true);
        FragmentJumper.addFragment(getHostActivity().getCustomSelfFragmentManager(), R.id.container, createTripTitleFragment, CreateTripTitleFragment.class.getName(), true);
    }

    @OnClick(R.id.il_trip_location)
    public void modifyLocation() {
        ChooseCityActivity.chooseLocation(CreateTripEditMainFragment.this, ChooseCityActivity.SERVICE_MODE, true);
    }

    @OnClick(R.id.il_trip_cates)
    public void modifyCates() {
        SelectTripCatesFragment selectTripCatesFragment = SelectTripCatesFragment.newInstance(mServiceInfo, true);
        FragmentJumper.addFragment(getHostActivity().getCustomSelfFragmentManager(), R.id.container, selectTripCatesFragment, SelectTripCatesFragment.class.getName(), true);
    }

    @OnClick(R.id.il_trip_desc)
    public void completeDesc() {
        CreateTripDescFragment createTripDescFragment = CreateTripDescFragment.newInstance(mServiceInfo);
        FragmentJumper.addFragment(getHostActivity().getCustomSelfFragmentManager(), R.id.container, createTripDescFragment, CreateTripDescFragment.class.getName(), true);
    }

    @OnClick(R.id.il_trip_price)
    public void completePrice() {
        CreateTripPriceSettingFragment createTripPriceSettingFragment = CreateTripPriceSettingFragment.newInstance(mServiceInfo);
        FragmentJumper.addFragment(getHostActivity().getCustomSelfFragmentManager(), R.id.container, createTripPriceSettingFragment, CreateTripPriceSettingFragment.class.getName(), true);
    }

    @OnClick(R.id.il_trip_other)
    public void completeOtherInfo() {
//        startActivity(new Intent(getHostActivity(),TripDemandInstructionActivity.class).putExtra(EDIT_KEY,true).putExtra(ServiceInfo.KEY,mServiceInfo));
        CreateTripOtherInfoFragment createTripOtherInfoFragment = CreateTripOtherInfoFragment.newInstance(mServiceInfo, true);
        FragmentJumper.addFragment(getHostActivity().getCustomSelfFragmentManager(), R.id.container, createTripOtherInfoFragment, CreateTripOtherInfoFragment.class.getName(), true);
    }

    @OnClick(R.id.agree_icon)
    public void setAgree() {
        if (isAgree) {
            agreeIcon.setTextColor(ResourceUtil.getColor(R.color.apple_7ecc1e));
            tvCommit.setEnabled(true);
            isAgree = false;
        } else {
            agreeIcon.setTextColor(ResourceUtil.getColor(R.color.ganshi_ded8d7));
            tvCommit.setEnabled(false);
            isAgree = true;
        }
    }

    @OnClick(R.id.tv_agree_term)
    public void jump2Agree() {
        TrackUtil.trackScreenName("/terms/insider");
        WebViewProxy.getInstance().browseWeb(getHostActivity(), H5UrlMaker.getInsiderAgreement());
    }

    @OnClick(R.id.commit)
    public void commit() {
        int type = checkUserInfo();
        if (type==TYPE_VALIDATE_ALL){
            commitTrip();
        }else{
//            CompleteBasicUserInfoActivity.startActivity(CreateTripEditMainFragment.this,mServiceInfo,userInfo,type);
            FormType formType = ServiceFormProxy.getInstance().hasFormInvalidType(mServiceInfo);
            boolean isValid = performInvalidAction(formType);
            if (!isValid) {
                return;
            }
            JumpSuccessUtil.navigete2CompletInsiderInfoPage(CreateTripEditMainFragment.this,mServiceInfo,userInfo,type,REQUEST_CODE_COMPLETE_POP);
        }
    }

    private int checkUserInfo(){
        int type=-1;
        CtUserInfo ctUserInfo = LoginInstance.getInstance().getUserInfo();
        if (this.userInfo !=null) {
            if (!this.userInfo.isFinished()) {
                type = 0;
            } else {
                int validType = this.userInfo.getValidType();

                if ((validType & CtUserInfo.VALIDATE_MASK_PHONE)!=CtUserInfo.VALIDATE_MASK_PHONE ) {
                    //手机未验证
                    type = 1;
                } else if (CtUserInfo.ID_CHECK_NONE== ctUserInfo.getIdCheckStatus()|| CtUserInfo.ID_CHECK_FAILED== ctUserInfo.getIdCheckStatus()/*CtUserInfo.VALIDATE_MASK_IDCARD != validType && CtUserInfo.VALIDATE_MASK_INTERNATION_CARD != validType*/) {
                    //身份Id未验证
                    type = 2;
                } else {
                    //全部验证完
                    type = 3;
                }
            }
        }
        return type;
    }

    public boolean performInvalidAction(FormType formType) {
        switch (formType) {
            case TITLE:
                Utils.shakeView(tripTitleLayout);
                break;
            case ADDRESS:
                Utils.shakeView(tripLocationLayout);
                break;
            case CATEGORY:
                Utils.shakeView(tripCatesLayout);
                break;
            case DESCRIPTION:
                Utils.shakeView(tripDescLayout);
                break;
            case OTHERS:
                Utils.shakeView(tripOtherLayout);
                break;
            case PAY:
                Utils.shakeView(tripPriceLayout);
                break;
            case MAIN_PIC:
                Utils.shakeView(mMainPicLayout);
                break;
            case PIC:
                ToastUtil.toast(getHostActivity(),getString(R.string.trip_photo_desc_1),0);
                break;
            default:
                return true;
        }
        return false;
    }

    @OnClick(R.id.main_pic_layout)
    public void mainPicSelect() {
        chooseMainImagePic();
    }

    public void chooseMainImagePic() {
        if (TextUtils.isEmpty(mServiceInfo.getBackPic()) && ArrayUtil.isNullOrEmpty(mServiceInfo.getPictureDescriptionList())) {
            EasyImageChooser.chooseImage(this, REQUEST_IMAGE_CODE, mServiceInfo.getPictureDescriptionList() == null ? 0 : mServiceInfo.getPictureDescriptionList().size());
        } else {
            navigateImageSelect(getHostActivity(), null);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE_CODE && resultCode == Activity.RESULT_OK) {
            ArrayList<String> mSelectPath = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
            navigateImageSelect(getHostActivity(), mSelectPath);
        } else if (resultCode == Activity.RESULT_OK && requestCode == ChooseCityActivity.REQUEST_CHOOSE_LOCATION) {
            String countryName = data.getStringExtra(ChooseCityActivity.COUNTRY_NAME_KEY);
            String countryAbbr = data.getStringExtra(ChooseCityActivity.COUNTRY_ABBR_KEY);
            String cityAbbr = data.getStringExtra(ChooseCityActivity.CITY_ABBR_KEY);
            String city = data.getStringExtra(ChooseCityActivity.CITY_NAME_KEY);
            String address = data.getStringExtra(ChooseCityActivity.ADDRESS_KEY);
            mServiceInfo.setAddress(address);
            mServiceInfo.setCountry(countryAbbr);
            mServiceInfo.setCity(city);
            mServiceInfo.setCityAbbr(cityAbbr);
            renderAddress();
        }
    }

    public void navigateImageSelect(BaseActivity activity, ArrayList<String> uploadList) {
        TripEditPicFragment tripEditPicFragment =
                TripEditPicFragment.getInstance(mServiceInfo, uploadList);
        activity.openFragment(tripEditPicFragment, R.id.container, TripEditPicFragment.class.getName(), true);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (!subscription.isUnsubscribed()){
            subscription.unsubscribe();
        }
    }

    private void onLeftPage() {
        String[] tip;
        if (mServiceInfo.isUnSync()) {
            tip = new String[]{ResourceUtil.getString(R.string.operation_save_as_draft), ResourceUtil.getString(R.string.operation_save_no), ResourceUtil.getString(R.string.operation_cancel)};
        } else if (mServiceInfo.isChecked()) {
            tip = new String[]{ResourceUtil.getString(R.string.operation_save_no), ResourceUtil.getString(R.string.operation_cancel)};
        } else if (mServiceInfo.isCheckFailed()) {
            tip = new String[]{ResourceUtil.getString(R.string.operation_save_as_draft), ResourceUtil.getString(R.string.operation_save_no), ResourceUtil.getString(R.string.operation_cancel)};
        } else if (mServiceInfo.isDraft()) {
            tip = new String[]{ResourceUtil.getString(R.string.operation_save_as_draft), ResourceUtil.getString(R.string.operation_save_no), ResourceUtil.getString(R.string.operation_cancel)};
        } else if (mServiceInfo.isChecking()) {
            tip = new String[]{ResourceUtil.getString(R.string.operation_save_as_draft), ResourceUtil.getString(R.string.operation_save_no), ResourceUtil.getString(R.string.operation_cancel)};
        } else {
            return;
        }

        final ChoiceDialog choiceDialog = ChoiceDialog.getInstance();
        List<MultiItem> multiItemList = new ArrayList<MultiItem>();
        for (String apiString : tip) {
            MultiItem multiItem = new MultiItem(apiString);
            if (apiString.equals(ResourceUtil.getString(R.string.operation_save_as_draft))) {
                multiItem.setColor(ResourceUtil.getThemeColor());
            } else {
                multiItem.setColor(ResourceUtil.getColor(R.color.qiaomai_7c706e));
            }
            multiItemList.add(multiItem);
        }
        choiceDialog.setDialogMessageVisibility(false);
        choiceDialog.setDialogTitleVisibility(false);
        choiceDialog.setDialogMultiStyleSetting(multiItemList, new ChoiceDialog.DialogStyleMultiCallback() {
            @Override
            public boolean onMultiItemClick(MultiItem multiItem, int position) {
                if (position == 0) {
                    if (mServiceInfo.isUnSync() || mServiceInfo.isCheckFailed() || mServiceInfo.isDraft() || mServiceInfo.isChecking()) {
                        if (ServiceFormProxy.getInstance().isFormComplete(mServiceInfo, FormType.TITLE)) {
                            showLoading();
                            mServiceInfo.setOnlineState(ServiceInfo.DRAFT_STATE);
                            mSaveAction = DRAFT_ACTION;
                            ServiceBusiness.commitServiceInfo(mApiProxy, mServiceInfo);
                        } else {
                            performInvalidAction(FormType.TITLE);
                        }
                    } else {
                        finishPage();
                    }
                } else if (position == 1) {
                    if (mServiceInfo.isUnSync() || mServiceInfo.isCheckFailed() || mServiceInfo.isDraft() || mServiceInfo.isChecking()) {
                        finishPage();
                    }

                }
                choiceDialog.dismiss();
                return true;
            }
        });
        showDialogFragment(choiceDialog);
    }

    private void finishPage() {
        getHostActivity().finish();
    }


    @Override
    public void onTopbarLeftPress() {
        onLeftPage();
    }

    @Override
    public void onTopbarRightPress() {
        previewTrip();
    }

    private void commitTrip() {
        FormType formType = ServiceFormProxy.getInstance().hasFormInvalidType(mServiceInfo);
        boolean isValid = performInvalidAction(formType);
        if (!isValid) {
            return;
        }
        showLoading();
        mServiceInfo.setOnlineState(ServiceInfo.ONLINE_STATE);
        mSaveAction = COMMIT_ACTION;
        ServiceBusiness.commitServiceInfo(mApiProxy, mServiceInfo);
    }

    private void previewTrip() {
        FormType formType = ServiceFormProxy.getInstance().hasFormInvalidType(mServiceInfo);
        boolean isValid = performInvalidAction(formType);
        if (!isValid) {
            return;
        }
        mServiceInfo.setOnlineState(ServiceInfo.DRAFT_STATE);
        mSaveAction = PREVIEW_ACTION;
        ServiceBusiness.commitServiceInfo(mApiProxy, mServiceInfo);
    }


    @Override
    public boolean onProxyResult(ProxyResult<?> result) {
        if (!isAdded()){
            return false;
        }
        CtApiResponse response = (CtApiResponse) result.getData();
        if (ApiManager.SERVICE_COMMIT.equals(response.getApiName())) {
            hideLoading();
            if (response.isResponseNormal()) {
                if (response.result instanceof ServiceInfo) {
                    ServiceInfo serviceInfo = (ServiceInfo) response.result;
                    mServiceInfo.setSid(serviceInfo.getSid());
                    handleSuccessResult(serviceInfo);
                }
            } else {
                if (!TextUtils.isEmpty(response.msg)) {
                    MessageUtils.showToast(response.msg);
                }
            }
        }else if (ApiManager.CHECK_BASIC_USER_INFO.equals(response.getApiName())){
            hideLoading();
            if (response.isResponseNormal()){
                if (response.result instanceof CtUserInfo) {
                    userInfo = (CtUserInfo) response.result;
                }
            }else{
                if (!TextUtils.isEmpty(response.msg)) {
                    MessageUtils.showToast(response.msg);
                }
            }
        }
        return true;
    }

    private void handleSuccessResult(ServiceInfo serviceInfo) {
        if (getContext() == null) {
            return;
        }
        LocalBroadcastManager.getInstance(AppCoreRuntime.context).sendBroadcast(
                new Intent(TripServiceListFragment.TRIP_SERVICE_STATUS_CHANGED));
        if (mSaveAction == COMMIT_ACTION) {
            SubmitSuccessModel.ContentType type = SubmitSuccessModel.ContentType.CREATE_TRIP;
            String title = ResourceUtil.getString(R.string.feedback_submit_suc);
            String content = ResourceUtil.getString(R.string.trip_state_submission_desc_1);
            String actionText = ResourceUtil.getString(R.string.operation_ok_1);
            SubmitSuccessModel submitSuccessModel = new SubmitSuccessModel(type, title, content, actionText);
            SubmitSuccessActivity.launchSuccessPage(getContext(), submitSuccessModel);
            finishPage();
        } else if (mSaveAction == DRAFT_ACTION) {
            finishPage();
        } else if (mSaveAction == PREVIEW_ACTION) {
            TrackUtil.trackScreenName("/insider/trips/preview");
            ServiceDetailActivity.startFinder(getContext(), serviceInfo.getSid(), ServiceInfo.STATUS_DRAFT);
        }

    }


    @Override
    public boolean onBackPressed(boolean fromKey) {
        onLeftPage();
        return true;
    }

}
